home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb / sprite / infcmd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-10  |  29.8 KB  |  1,166 lines

  1. /* Memory-access and commands for inferior process, for GDB.
  2.    Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. GDB is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GDB is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GDB; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include "defs.h"
  22. #include "param.h"
  23. #include "symtab.h"
  24. #include "frame.h"
  25. #include "inferior.h"
  26. #include "environ.h"
  27. #include "value.h"
  28.  
  29. #include <signal.h>
  30. #include <sys/param.h>
  31.  
  32. extern char *sys_siglist[];
  33.  
  34. #define ERROR_NO_INFERIOR \
  35.    if (inferior_pid == 0) error ("The program is not being run.");
  36.  
  37. /* String containing arguments to give to the program,
  38.    with a space added at the front.  Just a space means no args.  */
  39.  
  40. static char *inferior_args;
  41.  
  42. /* File name for default use for standard in/out in the inferior.  */
  43.  
  44. char *inferior_io_terminal;
  45.  
  46. /* Pid of our debugged inferior, or 0 if no inferior now.  */
  47.  
  48. int inferior_pid;
  49.  
  50. /* Last signal that the inferior received (why it stopped).  */
  51.  
  52. int stop_signal;
  53.  
  54. /* Address at which inferior stopped.  */
  55.  
  56. CORE_ADDR stop_pc;
  57.  
  58. /* Stack frame when program stopped.  */
  59.  
  60. FRAME_ADDR stop_frame_address;
  61.  
  62. /* Number of breakpoint it stopped at, or 0 if none.  */
  63.  
  64. int stop_breakpoint;
  65.  
  66. /* Nonzero if stopped due to a step command.  */
  67.  
  68. int stop_step;
  69.  
  70. /* Nonzero if stopped due to completion of a stack dummy routine.  */
  71.  
  72. int stop_stack_dummy;
  73.  
  74. /* Nonzero if stopped due to a random (unexpected) signal in inferior
  75.    process.  */
  76.  
  77. int stopped_by_random_signal;
  78.  
  79. /* Range to single step within.
  80.    If this is nonzero, respond to a single-step signal
  81.    by continuing to step if the pc is in this range.  */
  82.  
  83. CORE_ADDR step_range_start; /* Inclusive */
  84. CORE_ADDR step_range_end; /* Exclusive */
  85.  
  86. /* Stack frame address as of when stepping command was issued.
  87.    This is how we know when we step into a subroutine call,
  88.    and how to set the frame for the breakpoint used to step out.  */
  89.  
  90. FRAME_ADDR step_frame_address;
  91.  
  92. /* 1 means step over all subroutine calls.
  93.    -1 means step over calls to undebuggable functions.  */
  94.  
  95. int step_over_calls;
  96.  
  97. /* If stepping, nonzero means step count is > 1
  98.    so don't print frame next time inferior stops
  99.    if it stops due to stepping.  */
  100.  
  101. int step_multi;
  102.  
  103. /* Environment to use for running inferior,
  104.    in format described in environ.h.  */
  105.  
  106. struct environ *inferior_environ;
  107.  
  108. CORE_ADDR read_pc ();
  109. struct command_line *get_breakpoint_commands ();
  110. void breakpoint_clear_ignore_counts ();
  111.  
  112.  
  113. int
  114. have_inferior_p ()
  115. {
  116.   return inferior_pid != 0;
  117. }
  118.  
  119. static void 
  120. set_args_command (args)
  121.      char *args;
  122. {
  123.   free (inferior_args);
  124.   if (!args) args = "";
  125.   inferior_args = concat (" ", args, "");
  126. }
  127.  
  128. void
  129. tty_command (file, from_tty)
  130.      char *file;
  131.      int from_tty;
  132. {
  133.   if (file == 0)
  134.     error_no_arg ("terminal name for running target process");
  135.  
  136.   inferior_io_terminal = savestring (file, strlen (file));
  137. }
  138.  
  139. static void
  140. run_command (args, from_tty)
  141.      char *args;
  142.      int from_tty;
  143. {
  144.   extern char **environ;
  145.   register int i;
  146.   char *exec_file;
  147.   char *allargs;
  148.  
  149.   extern int sys_nerr;
  150.   extern char *sys_errlist[];
  151.   extern int errno;
  152.  
  153.   dont_repeat ();
  154.  
  155.   if (inferior_pid)
  156.     {
  157.       if (
  158.       !query ("The program being debugged has been started already.\n\
  159. Start it from the beginning? "))
  160.     error ("Program not restarted.");
  161.       kill_inferior ();
  162.     }
  163.  
  164. #if 0
  165.   /* On the other hand, some users want to do
  166.      break open
  167.      ignore 1 40
  168.      run
  169.      So it's not clear what is best.  */
  170.  
  171.   /* It is confusing to the user for ignore counts to stick around
  172.      from previous runs of the inferior.  So clear them.  */
  173.   breakpoint_clear_ignore_counts ();
  174. #endif
  175.  
  176.   exec_file = (char *) get_exec_file (1);
  177.  
  178.   if (remote_debugging)
  179.     {
  180.       if (from_tty)
  181.     {
  182.       printf ("Starting program: %s\n", exec_file);
  183.       fflush (stdout);
  184.     }
  185.     }
  186.   else
  187.     {
  188.       if (args)
  189.     set_args_command (args);
  190.  
  191.       if (from_tty)
  192.     {
  193.       printf ("Starting program: %s%s\n",
  194.           exec_file, inferior_args);
  195.       fflush (stdout);
  196.     }
  197. #ifdef sprite
  198.       allargs = concat ("exec debug ", exec_file, inferior_args);
  199. #else
  200.       allargs = concat ("exec ", exec_file, inferior_args);
  201. #endif
  202.       inferior_pid = create_inferior (allargs, environ_vector (inferior_environ));
  203.     }
  204.  
  205.   clear_proceed_status ();
  206.  
  207.   start_inferior ();
  208. }
  209.  
  210. void
  211. cont_command (proc_count_exp, from_tty)
  212.      char *proc_count_exp;
  213.      int from_tty;
  214. {
  215.   ERROR_NO_INFERIOR;
  216.  
  217.   clear_proceed_status ();
  218.  
  219.   /* If have argument, set proceed count of breakpoint we stopped at.  */
  220.  
  221.   if (stop_breakpoint > 0 && proc_count_exp)
  222.     {
  223.       set_ignore_count (stop_breakpoint,
  224.             parse_and_eval_address (proc_count_exp) - 1,
  225.             from_tty);
  226.       if (from_tty)
  227.     printf ("  ");
  228.     }
  229.  
  230.   if (from_tty)
  231.     printf ("Continuing.\n");
  232.  
  233.   proceed (-1, -1, 0);
  234. }
  235.  
  236. /* Step until outside of current statement.  */
  237. static void step_1 ();
  238.  
  239. static void
  240. step_command (count_string)
  241. {
  242.   step_1 (0, 0, count_string);
  243. }
  244.  
  245. /* Likewise, but skip over subroutine calls as if single instructions.  */
  246.  
  247. static void
  248. next_command (count_string)
  249. {
  250.   step_1 (1, 0, count_string);
  251. }
  252.  
  253. /* Likewise, but step only one instruction.  */
  254.  
  255. static void
  256. stepi_command (count_string)
  257. {
  258.   step_1 (0, 1, count_string);
  259. }
  260.  
  261. static void
  262. nexti_command (count_string)
  263. {
  264.   step_1 (1, 1, count_string);
  265. }
  266.  
  267. static void
  268. step_1 (skip_subroutines, single_inst, count_string)
  269.      int skip_subroutines;
  270.      int single_inst;
  271.      char *count_string;
  272. {
  273.   register int count = 1;
  274.  
  275.   ERROR_NO_INFERIOR;
  276.   count = count_string ? parse_and_eval_address (count_string) : 1;
  277.  
  278.   for (; count > 0; count--)
  279.     {
  280.       clear_proceed_status ();
  281.  
  282.       step_frame_address = FRAME_FP (get_current_frame ());
  283.  
  284.       if (! single_inst)
  285.     {
  286.       find_pc_line_pc_range (stop_pc, &step_range_start, &step_range_end);
  287.       if (step_range_end == 0)
  288.         {
  289.           int misc;
  290.  
  291.           misc = find_pc_misc_function (stop_pc);
  292.           terminal_ours ();
  293.           printf ("Current function has no line number information.\n");
  294.           fflush (stdout);
  295.  
  296.           /* No info or after _etext ("Can't happen") */
  297.           if (misc == -1 || misc == misc_function_count - 1)
  298.         error ("No data available on pc function.");
  299.  
  300.           printf ("Single stepping until function exit.\n");
  301.           fflush (stdout);
  302.  
  303.           step_range_start = misc_function_vector[misc].address;
  304.           step_range_end = misc_function_vector[misc + 1].address;
  305.         }
  306.     }
  307.       else
  308.     {
  309.       /* Say we are stepping, but stop after one insn whatever it does.
  310.          Don't step through subroutine calls even to undebuggable
  311.          functions.  */
  312.       step_range_start = step_range_end = 1;
  313.       if (!skip_subroutines)
  314.         step_over_calls = 0;
  315.     }
  316.  
  317.       if (skip_subroutines)
  318.     step_over_calls = 1;
  319.  
  320.       step_multi = (count > 1);
  321.       proceed (-1, -1, 1);
  322.       if (! stop_step)
  323.     break;
  324.     }
  325. }
  326.  
  327. /* Continue program at specified address.  */
  328.  
  329. static void
  330. jump_command (arg, from_tty)
  331.      char *arg;
  332.      int from_tty;
  333. {
  334.   register CORE_ADDR addr;
  335.   struct symtabs_and_lines sals;
  336.   struct symtab_and_line sal;
  337.  
  338.   ERROR_NO_INFERIOR;
  339.  
  340.   if (!arg)
  341.     error_no_arg ("starting address");
  342.  
  343.   sals = decode_line_spec_1 (arg, 1);
  344.   if (sals.nelts != 1)
  345.     {
  346.       error ("Unreasonable jump request");
  347.     }
  348.  
  349.   sal = sals.sals[0];
  350.   free (sals.sals);
  351.  
  352.   if (sal.symtab == 0 && sal.pc == 0)
  353.     error ("No source file has been specified.");
  354.  
  355.   if (sal.pc == 0)
  356.     sal.pc = find_line_pc (sal.symtab, sal.line);
  357.  
  358.   {
  359.     struct symbol *fn = get_frame_function (get_current_frame ());
  360.     struct symbol *sfn = find_pc_function (sal.pc);
  361.     if (fn != 0 && sfn != fn
  362.     && ! query ("Line %d is not in `%s'.  Jump anyway? ",
  363.             sal.line, SYMBOL_NAME (fn)))
  364.       error ("Not confirmed.");
  365.   }
  366.  
  367.   if (sal.pc == 0)
  368.     error ("No line %d in file \"%s\".", sal.line, sal.symtab->filename);
  369.  
  370.   addr = sal.pc;
  371.  
  372.   clear_proceed_status ();
  373.  
  374.   if (from_tty)
  375.     printf ("Continuing at 0x%x.\n", addr);
  376.  
  377.   proceed (addr, 0, 0);
  378. }
  379.  
  380. /* Continue program giving it specified signal.  */
  381.  
  382. static void
  383. signal_command (signum_exp, from_tty)
  384.      char *signum_exp;
  385.      int from_tty;
  386. {
  387.   register int signum;
  388.  
  389.   dont_repeat ();        /* Too dangerous.  */
  390.   ERROR_NO_INFERIOR;
  391.  
  392.   if (!signum_exp)
  393.     error_no_arg ("signal number");
  394.  
  395.   signum = parse_and_eval_address (signum_exp);
  396.  
  397.   clear_proceed_status ();
  398.  
  399.   if (from_tty)
  400.     printf ("Continuing with signal %d.\n", signum);
  401.  
  402.   proceed (stop_pc, signum, 0);
  403. }
  404.  
  405. /* Execute a "stack dummy", a piece of code stored in the stack
  406.    by the debugger to be executed in the inferior.
  407.  
  408.    To call: first, do PUSH_DUMMY_FRAME.
  409.    Then push the contents of the dummy.  It should end with a breakpoint insn.
  410.    Then call here, passing address at which to start the dummy.
  411.  
  412.    The contents of all registers are saved before the dummy frame is popped
  413.    and copied into the buffer BUFFER.
  414.  
  415.    The dummy's frame is automatically popped whenever that break is hit.
  416.    If that is the first time the program stops, run_stack_dummy
  417.    returns to its caller with that frame already gone.
  418.    Otherwise, the caller never gets returned to.  */
  419.  
  420. /* 4 => return instead of letting the stack dummy run.  */
  421.  
  422. static int stack_dummy_testing = 0;
  423.  
  424. void
  425. run_stack_dummy (addr, buffer)
  426.      CORE_ADDR addr;
  427.      REGISTER_TYPE *buffer;
  428. {
  429.   /* Now proceed, having reached the desired place.  */
  430.   clear_proceed_status ();
  431.   if (stack_dummy_testing & 4)
  432.     {
  433.       POP_FRAME;
  434.       return;
  435.     }
  436.   proceed (addr, 0, 0);
  437.  
  438.   if (!stop_stack_dummy)
  439.     error ("Cannot continue previously requested operation.");
  440.  
  441.   /* On return, the stack dummy has been popped already.  */
  442.  
  443.   bcopy (stop_registers, buffer, sizeof stop_registers);
  444. }
  445.  
  446. /* Proceed until we reach the given line as argument or exit the
  447.    function.  When called with no argument, proceed until we reach a
  448.    different source line with pc greater than our current one or exit
  449.    the function.  We skip calls in both cases.
  450.  
  451.    The effect of this command with an argument is identical to setting
  452.    a momentary breakpoint at the line specified and executing
  453.    "finish".
  454.  
  455.    Note that eventually this command should probably be changed so
  456.    that only source lines are printed out when we hit the breakpoint
  457.    we set.  I'm going to postpone this until after a hopeful rewrite
  458.    of wait_for_inferior and the proceed status code. -- randy */
  459.  
  460. void
  461. until_next_command (arg, from_tty)
  462.      char *arg;
  463.      int from_tty;
  464. {
  465.   FRAME frame;
  466.   CORE_ADDR pc;
  467.   struct symbol *func;
  468.   struct symtab_and_line sal;
  469.     
  470.   clear_proceed_status ();
  471.  
  472.   frame = get_current_frame ();
  473.  
  474.   /* Step until either exited from this function or greater
  475.      than the current line (if in symbolic section) or pc (if
  476.      not). */
  477.  
  478.   pc = read_pc ();
  479.   func = find_pc_function (pc);
  480.   
  481.   if (!func)
  482.     {
  483.       int misc_func = find_pc_misc_function (pc);
  484.       
  485.       if (misc_func != -1)
  486.     error ("Execution is not within a known function.");
  487.       
  488.       step_range_start = misc_function_vector[misc_func].address;
  489.       step_range_end = pc;
  490.     }
  491.   else
  492.     {
  493.       sal = find_pc_line (pc, 0);
  494.       
  495.       step_range_start = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
  496.       step_range_end = sal.end;
  497.     }
  498.   
  499.   step_over_calls = 1;
  500.   step_frame_address = FRAME_FP (frame);
  501.   
  502.   step_multi = 0;        /* Only one call to proceed */
  503.   
  504.   proceed (-1, -1, 1);
  505. }
  506.  
  507. void 
  508. until_command (arg, from_tty)
  509.      char *arg;
  510.      int from_tty;
  511. {
  512.   if (!have_inferior_p ())
  513.     error ("The program is not being run.");
  514.  
  515.   if (arg)
  516.     until_break_command (arg, from_tty);
  517.   else
  518.     until_next_command (arg, from_tty);
  519. }
  520.  
  521. /* "finish": Set a temporary breakpoint at the place
  522.    the selected frame will return to, then continue.  */
  523.  
  524. static void
  525. finish_command (arg, from_tty)
  526.      char *arg;
  527.      int from_tty;
  528. {
  529.   struct symtab_and_line sal;
  530.   register FRAME frame;
  531.   struct frame_info *fi;
  532.   register struct symbol *function;
  533.  
  534.   if (!have_inferior_p ())
  535.     error ("The program is not being run.");
  536.   if (arg)
  537.     error ("The \"finish\" command does not take any arguments.");
  538.  
  539.   frame = get_prev_frame (selected_frame);
  540.   if (frame == 0)
  541.     error ("\"finish\" not meaningful in the outermost frame.");
  542.  
  543.   clear_proceed_status ();
  544.  
  545.   fi = get_frame_info (frame);
  546.   sal = find_pc_line (fi->pc, 0);
  547.   sal.pc = fi->pc;
  548.   set_momentary_breakpoint (sal, frame);
  549.  
  550.   /* Find the function we will return from.  */
  551.  
  552.   fi = get_frame_info (selected_frame);
  553.   function = find_pc_function (fi->pc);
  554.  
  555.   if (from_tty)
  556.     {
  557.       printf ("Run till exit from ");
  558.       print_selected_frame ();
  559.     }
  560.  
  561.   proceed (-1, -1, 0);
  562.  
  563.   if (stop_breakpoint == -3 && function != 0)
  564.     {
  565.       struct type *value_type;
  566.       register value val;
  567.       CORE_ADDR funcaddr;
  568.  
  569.       value_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
  570.       if (!value_type)
  571.     fatal ("internal: finish_command: function has no target type");
  572.       
  573.       if (TYPE_CODE (value_type) == TYPE_CODE_VOID)
  574.     return;
  575.  
  576.       funcaddr = BLOCK_START (SYMBOL_BLOCK_VALUE (function));
  577.  
  578.       val = value_being_returned (value_type, stop_registers,
  579.                   using_struct_return (function,
  580.                                funcaddr,
  581.                                value_type));
  582.  
  583.       printf ("Value returned is $%d = ", record_latest_value (val));
  584.       value_print (val, stdout, 0, Val_no_prettyprint);
  585.       putchar ('\n');
  586.     }
  587. }
  588.  
  589. static void
  590. program_info ()
  591. {
  592.   if (inferior_pid == 0)
  593.     {
  594.       printf ("The program being debugged is not being run.\n");
  595.       return;
  596.     }
  597.  
  598.   printf ("Program being debugged is in process %d, stopped at 0x%x.\n",
  599.       inferior_pid, stop_pc);
  600.   if (stop_step)
  601.     printf ("It stopped after being stepped.\n");
  602.   else if (stop_breakpoint > 0)
  603.     printf ("It stopped at breakpoint %d.\n", stop_breakpoint);
  604.   else if (stop_signal)
  605.     printf ("It stopped with signal %d (%s).\n",
  606.         stop_signal, sys_siglist[stop_signal]);
  607.  
  608.   printf ("\nType \"info stack\" or \"info reg\" for more information.\n");
  609. }
  610.  
  611. static void
  612. environment_info (var)
  613.      char *var;
  614. {
  615.   if (var)
  616.     {
  617.       register char *val = get_in_environ (inferior_environ, var);
  618.       if (val)
  619.     printf ("%s = %s\n", var, val);
  620.       else
  621.     printf ("Environment variable \"%s\" not defined.\n", var);
  622.     }
  623.   else
  624.     {
  625.       register char **vector = environ_vector (inferior_environ);
  626.       while (*vector)
  627.     printf ("%s\n", *vector++);
  628.     }
  629. }
  630.  
  631. static void
  632. set_environment_command (arg)
  633.      char *arg;
  634. {
  635.   register char *p, *val, *var;
  636.   int nullset = 0;
  637.  
  638.   if (arg == 0)
  639.     error_no_arg ("environment variable and value");
  640.  
  641.   /* Find seperation between variable name and value */
  642.   p = (char *) index (arg, '=');
  643.   val = (char *) index (arg, ' ');
  644.  
  645.   if (p != 0 && val != 0)
  646.     {
  647.       /* We have both a space and an equals.  If the space is before the
  648.      equals and the only thing between the two is more space, use
  649.      the equals */
  650.       if (p > val)
  651.     while (*val == ' ')
  652.       val++;
  653.  
  654.       /* Take the smaller of the two.  If there was space before the
  655.      "=", they will be the same right now. */
  656.       p = arg + min (p - arg, val - arg);
  657.     }
  658.   else if (val != 0 && p == 0)
  659.     p = val;
  660.  
  661.   if (p == arg)
  662.     error_no_arg ("environment variable to set");
  663.  
  664.   if (p == 0 || p[1] == 0)
  665.     {
  666.       nullset = 1;
  667.       if (p == 0)
  668.     p = arg + strlen (arg);    /* So that savestring below will work */
  669.     }
  670.   else
  671.     {
  672.       /* Not setting variable value to null */
  673.       val = p + 1;
  674.       while (*val == ' ' || *val == '\t')
  675.     val++;
  676.     }
  677.  
  678.   while (p != arg && (p[-1] == ' ' || p[-1] == '\t')) p--;
  679.  
  680.   var = savestring (arg, p - arg);
  681.   if (nullset)
  682.     {
  683.       printf ("Setting environment variable \"%s\" to null value.\n", var);
  684.       set_in_environ (inferior_environ, var, "");
  685.     }
  686.   else
  687.     set_in_environ (inferior_environ, var, val);
  688.   free (var);
  689. }
  690.  
  691. static void
  692. unset_environment_command (var, from_tty)
  693.      char *var;
  694.      int from_tty;
  695. {
  696.   if (var == 0)
  697.     /* If there is no argument, delete all environment variables.
  698.        Ask for confirmation if reading from the terminal.  */
  699.     if (!from_tty || query ("Delete all environment variables? "))
  700.       {
  701.     free_environ (inferior_environ);
  702.     inferior_environ = make_environ ();
  703.       }
  704.  
  705.   unset_in_environ (inferior_environ, var);
  706. }
  707.  
  708. /* Read an integer from debugged memory, given address and number of bytes.  */
  709.  
  710. long
  711. read_memory_integer (memaddr, len)
  712.      CORE_ADDR memaddr;
  713.      int len;
  714. {
  715.   char cbuf;
  716.   short sbuf;
  717.   int ibuf;
  718.   long lbuf;
  719.   int result_err;
  720.   extern int sys_nerr;
  721.   extern char *sys_errlist[];
  722.  
  723.   if (len == sizeof (char))
  724.     {
  725.       result_err = read_memory (memaddr, &cbuf, len);
  726.       if (result_err)
  727.     error ("Error reading memory address 0x%x: %s (%d).",
  728.            memaddr, (result_err < sys_nerr ?
  729.              sys_errlist[result_err] :
  730.              "uknown error"), result_err);
  731.       return cbuf;
  732.     }
  733.   if (len == sizeof (short))
  734.     {
  735.       result_err = read_memory (memaddr, &sbuf, len);
  736.       if (result_err)
  737.     error ("Error reading memory address 0x%x: %s (%d).",
  738.            memaddr, (result_err < sys_nerr ?
  739.              sys_errlist[result_err] :
  740.              "uknown error"), result_err);
  741.       return sbuf;
  742.     }
  743.   if (len == sizeof (int))
  744.     {
  745.       result_err = read_memory (memaddr, &ibuf, len);
  746.       if (result_err)
  747.     error ("Error reading memory address 0x%x: %s (%d).",
  748.            memaddr, (result_err < sys_nerr ?
  749.              sys_errlist[result_err] :
  750.              "uknown error"), result_err);
  751.       return ibuf;
  752.     }
  753.   if (len == sizeof (lbuf))
  754.     {
  755.       result_err = read_memory (memaddr, &lbuf, len);
  756.       if (result_err)
  757.     error ("Error reading memory address 0x%x: %s (%d).",
  758.            memaddr, (result_err < sys_nerr ?
  759.              sys_errlist[result_err] :
  760.              "uknown error"), result_err);
  761.       return lbuf;
  762.     }
  763.   error ("Cannot handle integers of %d bytes.", len);
  764. }
  765.  
  766. CORE_ADDR
  767. read_pc ()
  768. {
  769.   return (CORE_ADDR) read_register (PC_REGNUM);
  770. }
  771.  
  772. void
  773. write_pc (val)
  774.      CORE_ADDR val;
  775. {
  776.   write_register (PC_REGNUM, (long) val);
  777. #ifdef NPC_REGNUM
  778.   write_register (NPC_REGNUM, (long) val+4);
  779. #endif
  780. }
  781.  
  782. char *reg_names[] = REGISTER_NAMES;
  783.  
  784. /* Print out the machine register regnum. If regnum is -1,
  785.    print all registers.
  786.    For most machines, having all_registers_info() print the
  787.    register(s) one per line is good enough. If a different format
  788.    is required, (eg, for SPARC or Pyramid 90x, which both have
  789.    lots of regs), or there is an existing convention for showing
  790.    all the registers, define the macro DO_REGISTERS_INFO(regnum)
  791.    to provide that format.  */  
  792. #if !defined (DO_REGISTERS_INFO)
  793. #define DO_REGISTERS_INFO(regnum) do_registers_info(regnum)
  794. static void do_registers_info (regnum)
  795.      int regnum;
  796. {
  797.   register int i;
  798.  
  799.   if (regnum == -1)
  800.     printf_filtered (
  801.       "Register       Contents (relative to selected stack frame)\n\n");
  802.  
  803.   for (i = 0; i < NUM_REGS; i++)
  804.     {
  805.       unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE];
  806.       unsigned char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
  807.       REGISTER_TYPE val;
  808.  
  809.       if (regnum != -1 && i != regnum)
  810.     continue;
  811.  
  812.       /* Get the data in raw format, then convert also to virtual format.  */
  813.       read_relative_register_raw_bytes (i, raw_buffer);
  814.       REGISTER_CONVERT_TO_VIRTUAL (i, raw_buffer, virtual_buffer);
  815.  
  816.       fputs_filtered (reg_names[i], stdout);
  817.       print_spaces_filtered (15 - strlen (reg_names[i]), stdout);
  818.  
  819.       /* If virtual format is floating, print it that way.  */
  820.       if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT
  821.       && ! INVALID_FLOAT (virtual_buffer, REGISTER_VIRTUAL_SIZE (i)))
  822.     val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0,
  823.            stdout, 0, 1, 0, Val_pretty_default);
  824.       /* Else if virtual format is too long for printf,
  825.      print in hex a byte at a time.  */
  826.       else if (REGISTER_VIRTUAL_SIZE (i) > sizeof (long))
  827.     {
  828.       register int j;
  829.       printf_filtered ("0x");
  830.       for (j = 0; j < REGISTER_VIRTUAL_SIZE (i); j++)
  831.         printf_filtered ("%02x", virtual_buffer[j]);
  832.     }
  833.       /* Else print as integer in hex and in decimal.  */
  834.       else
  835.     {
  836.       long val;
  837.  
  838.       bcopy (virtual_buffer, &val, sizeof (long));
  839.       if (val == 0)
  840.         printf_filtered ("0");
  841.       else
  842.         printf_filtered ("0x%08x  %d", val, val);
  843.     }
  844.  
  845.       /* If register has different raw and virtual formats,
  846.      print the raw format in hex now.  */
  847.  
  848.       if (REGISTER_CONVERTIBLE (i))
  849.     {
  850.       register int j;
  851.  
  852.       printf_filtered ("  (raw 0x");
  853.       for (j = 0; j < REGISTER_RAW_SIZE (i); j++)
  854.         printf_filtered ("%02x", raw_buffer[j]);
  855.       printf_filtered (")");
  856.     }
  857.       printf_filtered ("\n");
  858.     }
  859. }
  860. #endif /* no DO_REGISTERS_INFO.  */
  861.  
  862. static void
  863. registers_info (addr_exp)
  864.      char *addr_exp;
  865. {
  866.   int regnum;
  867.  
  868.   if (!have_inferior_p () && !have_core_file_p ())
  869.     error ("No inferior or core file");
  870.  
  871.   if (addr_exp)
  872.     {
  873.       if (*addr_exp >= '0' && *addr_exp <= '9')
  874.     regnum = atoi (addr_exp);
  875.       else
  876.     {
  877.       register char *p = addr_exp;
  878.       if (p[0] == '$')
  879.         p++;
  880.       for (regnum = 0; regnum < NUM_REGS; regnum++)
  881.         if (!strcmp (p, reg_names[regnum]))
  882.           break;
  883.       if (regnum == NUM_REGS)
  884.         error ("%s: invalid register name.", addr_exp);
  885.     }
  886.     }
  887.   else
  888.     regnum = -1;
  889.  
  890.   DO_REGISTERS_INFO(regnum);
  891. }
  892.  
  893. #ifdef ATTACH_DETACH
  894. #define PROCESS_ATTACH_ALLOWED 1
  895. #else
  896. #define PROCESS_ATTACH_ALLOWED 0
  897. #endif
  898. /*
  899.  * TODO:
  900.  * Should save/restore the tty state since it might be that the
  901.  * program to be debugged was started on this tty and it wants
  902.  * the tty in some state other than what we want.  If it's running
  903.  * on another terminal or without a terminal, then saving and
  904.  * restoring the tty state is a harmless no-op.
  905.  * This only needs to be done if we are attaching to a process.
  906.  */
  907.  
  908. /*
  909.  * attach_command --
  910.  * takes a program started up outside of gdb and ``attaches'' to it.
  911.  * This stops it cold in its tracks and allows us to start tracing it.
  912.  * For this to work, we must be able to send the process a
  913.  * signal and we must have the same effective uid as the program.
  914.  */
  915. static void
  916. attach_command (args, from_tty)
  917.      char *args;
  918.      int from_tty;
  919. {
  920.   char *exec_file;
  921.   int pid;
  922.   int remote = 0;
  923.  
  924.   dont_repeat();
  925.  
  926.   if (!args)
  927.     error_no_arg ("process-id or device file to attach");
  928.  
  929.   while (*args == ' ' || *args == '\t') args++;
  930.  
  931.   if (args[0] == '/')
  932.     remote = 1;
  933.   else
  934. #ifndef ATTACH_DETACH
  935.     error ("Can't attach to a process on this machine.");
  936. #else
  937. #ifdef sprite
  938.     /*
  939.      * Since Sprite pids are displayed in hex the atoi doesn't work. We
  940.      * allow the attach command to use any C expression.
  941.      */
  942.     pid = value_as_long (evaluate_expression (parse_c_expression (args)));
  943. #else
  944.     pid = atoi (args);
  945. #endif /* sprite */
  946. #endif /* ATTACH_DETACH */
  947.  
  948.   if (inferior_pid)
  949.     {
  950.       if (query ("A program is being debugged already.  Kill it? "))
  951.     kill_inferior ();
  952.       else
  953.     error ("Inferior not killed.");
  954.     }
  955.  
  956.   exec_file = (char *) get_exec_file (1);
  957.  
  958.   if (from_tty)
  959.     {
  960.       if (remote)
  961.     printf ("Attaching remote machine\n");
  962.       else
  963.     printf ("Attaching program: %s pid %d\n",
  964.         exec_file, pid);
  965.       fflush (stdout);
  966.     }
  967.  
  968. #ifdef ATTACH_DETACH
  969.   if (remote)
  970.     {
  971. #endif
  972.       remote_open (args, from_tty);
  973.       start_remote ();
  974. #ifdef ATTACH_DETACH
  975.     }
  976.   else
  977.     attach_program (pid);
  978. #endif
  979. }
  980.  
  981. /*
  982.  * detach_command --
  983.  * takes a program previously attached to and detaches it.
  984.  * The program resumes execution and will no longer stop
  985.  * on signals, etc.  We better not have left any breakpoints
  986.  * in the program or it'll die when it hits one.  For this
  987.  * to work, it may be necessary for the process to have been
  988.  * previously attached.  It *might* work if the program was
  989.  * started via the normal ptrace (PTRACE_TRACEME).
  990.  */
  991.  
  992. static void
  993. detach_command (args, from_tty)
  994.      char *args;
  995.      int from_tty;
  996. {
  997.   int signal = 0;
  998.  
  999. #ifdef ATTACH_DETACH
  1000.   if (inferior_pid)
  1001.     {
  1002.       if (from_tty)
  1003.     {
  1004.       char *exec_file = (char *)get_exec_file (0);
  1005.       if (exec_file == 0)
  1006.         exec_file = "";
  1007.       printf ("Detaching program: %s pid %d\n",
  1008.           exec_file, inferior_pid);
  1009.       fflush (stdout);
  1010.     }
  1011.       if (args)
  1012.     signal = atoi (args);
  1013.       
  1014.       detach (signal);
  1015.       inferior_pid = 0;
  1016.     }
  1017.   else
  1018. #endif
  1019.     {
  1020.       if (!remote_debugging)
  1021.     error ("Not currently attached to subsidiary or remote process.");
  1022.  
  1023.       if (args)
  1024.     error ("Argument given to \"detach\" when remotely debugging.");
  1025.       
  1026.       remote_close (from_tty);
  1027.     }
  1028. }
  1029.  
  1030. /* ARGUSUED */
  1031. static void
  1032. float_info (addr_exp)
  1033.      char *addr_exp;
  1034. {
  1035. #ifdef FLOAT_INFO
  1036.     FLOAT_INFO;
  1037. #else
  1038.     printf ("No floating point info available for this processor.\n");
  1039. #endif
  1040. }
  1041.  
  1042. extern struct cmd_list_element *setlist, *deletelist;
  1043.  
  1044. void
  1045. _initialize_infcmd ()
  1046. {
  1047.   add_com ("tty", class_run, tty_command,
  1048.        "Set terminal for future runs of program being debugged.");
  1049.  
  1050.   add_cmd ("args", class_run, set_args_command,
  1051.        "Specify arguments to give program being debugged when it is started.\n\
  1052. Follow this command with any number of args, to be passed to the program.",
  1053.        &setlist);
  1054.  
  1055.   add_info ("environment", environment_info,
  1056.         "The environment to give the program, or one variable's value.\n\
  1057. With an argument VAR, prints the value of environment variable VAR to\n\
  1058. give the program being debugged.  With no arguments, prints the entire\n\
  1059. environment to be given to the program.");
  1060.  
  1061.   add_cmd ("environment", class_run, unset_environment_command,
  1062.        "Cancel environment variable VAR for the program.\n\
  1063. This does not affect the program until the next \"run\" command.",
  1064.        &deletelist);
  1065.  
  1066.   add_cmd ("environment", class_run, set_environment_command,
  1067.        "Set environment variable value to give the program.\n\
  1068. Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
  1069. VALUES of environment variables are uninterpreted strings.\n\
  1070. This does not affect the program until the next \"run\" command.",
  1071.        &setlist);
  1072.  
  1073. #ifdef ATTACH_DETACH
  1074.  add_com ("attach", class_run, attach_command,
  1075.         "Attach to a process that was started up outside of GDB.\n\
  1076. This command may take as argument a process id or a device file.\n\
  1077. For a process id, you must have permission to send the process a signal,\n\
  1078. and it must have the same effective uid as the debugger.\n\
  1079. For a device file, the file must be a connection to a remote debug server.\n\n\
  1080. Before using \"attach\", you must use the \"exec-file\" command\n\
  1081. to specify the program running in the process,\n\
  1082. and the \"symbol-file\" command to load its symbol table.");
  1083. #else
  1084.  add_com ("attach", class_run, attach_command,
  1085.         "Attach to a process that was started up outside of GDB.\n\
  1086. This commands takes as an argument the name of a device file.\n\
  1087. This file must be a connection to a remote debug server.\n\n\
  1088. Before using \"attach\", you must use the \"exec-file\" command\n\
  1089. to specify the program running in the process,\n\
  1090. and the \"symbol-file\" command to load its symbol table.");
  1091. #endif
  1092.   add_com ("detach", class_run, detach_command,
  1093.        "Detach the process previously attached.\n\
  1094. The process is no longer traced and continues its execution.");
  1095.  
  1096.   add_com ("signal", class_run, signal_command,
  1097.        "Continue program giving it signal number SIGNUMBER.");
  1098.  
  1099.   add_com ("stepi", class_run, stepi_command,
  1100.        "Step one instruction exactly.\n\
  1101. Argument N means do this N times (or till program stops for another reason).");
  1102.   add_com_alias ("si", "stepi", class_alias, 0);
  1103.  
  1104.   add_com ("nexti", class_run, nexti_command,
  1105.        "Step one instruction, but proceed through subroutine calls.\n\
  1106. Argument N means do this N times (or till program stops for another reason).");
  1107.   add_com_alias ("ni", "nexti", class_alias, 0);
  1108.  
  1109.   add_com ("finish", class_run, finish_command,
  1110.        "Execute until selected stack frame returns.\n\
  1111. Upon return, the value returned is printed and put in the value history.");
  1112.  
  1113.   add_com ("next", class_run, next_command,
  1114.        "Step program, proceeding through subroutine calls.\n\
  1115. Like the \"step\" command as long as subroutine calls do not happen;\n\
  1116. when they do, the call is treated as one instruction.\n\
  1117. Argument N means do this N times (or till program stops for another reason).");
  1118.   add_com_alias ("n", "next", class_run, 1);
  1119.  
  1120.   add_com ("step", class_run, step_command,
  1121.        "Step program until it reaches a different source line.\n\
  1122. Argument N means do this N times (or till program stops for another reason).");
  1123.   add_com_alias ("s", "step", class_run, 1);
  1124.  
  1125.   add_com ("until", class_run, until_command,
  1126.        "Execute until the program reaches a source line greater than the current\n\
  1127. or a specified line or address or function (same args as break command).\n\
  1128. Execution will also stop upon exit from the current stack frame.");
  1129.   add_com_alias ("u", "until", class_run, 1);
  1130.   
  1131.   add_com ("jump", class_run, jump_command,
  1132.        "Continue program being debugged at specified line or address.\n\
  1133. Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
  1134. for an address to start at.");
  1135.  
  1136.   add_com ("cont", class_run, cont_command,
  1137.        "Continue program being debugged, after signal or breakpoint.\n\
  1138. If proceeding from breakpoint, a number N may be used as an argument:\n\
  1139. then the same breakpoint won't break until the Nth time it is reached.");
  1140.   add_com_alias ("c", "cont", class_run, 1);
  1141.  
  1142.   add_com ("run", class_run, run_command,
  1143.        "Start debugged program.  You may specify arguments to give it.\n\
  1144. Args may include \"*\", or \"[...]\"; they are expanded using \"sh\".\n\
  1145. Input and output redirection with \">\", \"<\", or \">>\" are also allowed.\n\n\
  1146. With no arguments, uses arguments last specified (with \"run\" or \"set args\".\n\
  1147. To cancel previous arguments and run with no arguments,\n\
  1148. use \"set args\" without arguments.");
  1149.   add_com_alias ("r", "run", class_run, 1);
  1150.  
  1151.   add_info ("registers", registers_info,
  1152.         "List of registers and their contents, for selected stack frame.\n\
  1153. Register name as argument means describe only that register.");
  1154.  
  1155.   add_info ("program", program_info,
  1156.         "Execution status of the program.");
  1157.  
  1158.   add_info ("float", float_info,
  1159.         "Print the status of the floating point unit\n");
  1160.  
  1161.   inferior_args = savestring (" ", 1); /* By default, no args.  */
  1162.   inferior_environ = make_environ ();
  1163.   init_environ (inferior_environ);
  1164. }
  1165.  
  1166.